package com.jyw.web.controller.system;

import com.jyw.common.annotation.Anonymous;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.domain.model.LoginUser;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.DownloadUtil;
import com.jyw.framework.web.service.TokenService;
import com.jyw.system.domain.vo.SimpleFileVo;
import com.jyw.system.service.ISysFileInfoService;
import io.minio.GetObjectResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * @author 沈峻
 * @ClassName SysFileController
 * @Description 系统文件管理
 * @Date 2023/8/21 17:15
 **/
@RestController
@RequestMapping("/system/file")
@Api(tags = "系统文件管理")
public class SysFileController {

    @Resource
    private ISysFileInfoService sysFileInfoService;

    @Resource
    private TokenService tokenService;


    @PostMapping("upload")
    @ApiOperation("文件上传")
    public CommonResult<List<SimpleFileVo>> upload(@RequestParam("file") MultipartFile[] files) {
        //文件上传
        List<SimpleFileVo> upload = sysFileInfoService.upload(files);
        return CommonResult.success(upload);
    }

    /**
     * 文件预览地址
     *
     * @param token
     * @return
     * @throws Exception
     */
    @GetMapping("getFile/**")
    @ApiOperation("文件流获取")
    @Anonymous
    public ResponseEntity<InputStreamResource> previewFile(@RequestParam("token") String token, HttpServletRequest request) throws Exception {
        if(!ObjectUtils.isEmpty(token)){
            //token校验
            LoginUser loginUser = tokenService.getLoginUser(token);
            if(ObjectUtils.isEmpty(loginUser)){
                throw new ServiceException("token已失效");
            }
            //获取文件
            String fullPath = request.getRequestURI();
            String path = fullPath.substring(fullPath.indexOf("system/file/getFile/")+"system/file/getFile/".length());
            InputStream inputStream = sysFileInfoService.download(path);
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_TYPE, ((GetObjectResponse)inputStream).headers().get(HttpHeaders.CONTENT_TYPE));
            headers.add(HttpHeaders.CONTENT_LENGTH, ((GetObjectResponse)inputStream).headers().get(HttpHeaders.CONTENT_LENGTH));
            return ResponseEntity.ok().headers(headers).body(new InputStreamResource(inputStream));
        }
        throw new ServiceException("token不能为空");
    }

    /**
     * 文件预览地址
     *
     * @param url
     * @return
     * @throws Exception
     */
    @GetMapping("previewFile")
    @ApiOperation("文件预览地址")
    public CommonResult previewFile(String url) throws Exception {
        String fileUrl = sysFileInfoService.previewFile(url);
        return CommonResult.success("成功！", fileUrl);
    }

    /**
     * 文件下载
     *
     * @param url
     * @param response
     * @throws Exception
     */
    @GetMapping("download")
    @ApiOperation("文件下载")
    public void download(String url, HttpServletResponse response) throws Exception {
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        InputStream inputStream = sysFileInfoService.download(url);
        DownloadUtil.download(inputStream, fileName, response);
    }
}
